﻿//for examples of use in an actual game see Nat Geo fight science loader
import mx.utils.Delegate;
import arkadium.XMLFile;
import FightScienceLoader_Code.DataManager;
import FightScienceLoader_Code.Main;

class FightScienceLoader_Code.UniLeaderboard extends MovieClip
{
	
	public var xmlGameEnd:XMLFile
	private var _sdtLeaderRank:TextField;
	private var _sdtLeaderNames:TextField;
	private var _sdtLeaderScores:TextField;
	private var _sbtMore:Button;
	
	public function UniLeaderboard(){
		onInitialize()
	}
	
	public function onInitialize(){
		this.gotoAndStop(1);
		xmlGameEnd = new XMLFile ();
		_sbtMore.onPress = Delegate.create( this, openMoreLeaderboards );
		_sbtMore.onRollOver  = function(){ Main.getInstance()._cSoundManager.rolloverSoundStart();   }
		
	}
	
	public function leaderboardLoaded(){
		trace("LOADED" + _parent);
		this.gotoAndStop(2);
		decodeXml();
	}
	
	public function sendAndDisplay(sUserName:String,nCumulativeScore:Number){
		var sLeaderBoardURL = Main.getInstance()._cAppConfigFile.getData("leaderboardUrl");
		if(sLeaderBoardURL==undefined)sLeaderBoardURL = "http://72.3.138.97/ngservice/leaderboard.aspx"//"http://testing.arkadium.com/arkadium_leaderboard/leaderboard.aspx"
		if(sUserName == undefined)sUserName = "AAA";
		var sParams = Main.getInstance()._cEncryptionManager.encryptParams('username='+sUserName+'&score='+nCumulativeScore+'&top=9&showMyRank=1');
		trace("sParams"+sParams );
		xmlGameEnd.loadFile( this, leaderboardLoaded, sLeaderBoardURL+ "?params="+sParams );
	}
	//helpText = (_root.leaderboardUrl+ "?params="+_root.TEAEncrypt('freeplayid='+_root.freeplayId+'&score='+_parent.txtScore,_root.tKey));

	
	public function displayLeaderboard(){
		var sLeaderBoardURL = Main.getInstance()._cAppConfigFile.getData("leaderboardUrl");
		if(sLeaderBoardURL==undefined)sLeaderBoardURL = "http://72.3.138.97/ngservice/leaderboard.aspx"//"http://testing.arkadium.com/arkadium_leaderboard/leaderboard.aspx"
		var sParams = Main.getInstance()._cEncryptionManager.encryptParams('&top=10');
		trace("sParams"+sParams );
		xmlGameEnd.loadFile( this, leaderboardLoaded, sLeaderBoardURL+ "?params="+sParams );
	}
	
	public function decodeXml(){
		trace("trying to decode xml")
		var leaderRank = "";
		var leaderNames = "<font color='#FFFFFF' style='bold'>";
		var leaderScores = "<font color='#FFFFFF' style='bold'>";
		var myLength = 9;
		var attachtext = "";
		//helpText += xmlGameEnd;
		if(xmlGameEnd.firstChild.childNodes.length == undefined) myLength = 9;
		else myLength = xmlGameEnd.firstChild.childNodes.length
		for(var count = 0;count<myLength;count++){
			attachtext = "";
			if(xmlGameEnd.firstChild.childNodes[count].attributes.isCurrentUser == 1){
				attachtext = "*";
				if(xmlGameEnd.firstChild.childNodes[count].attributes.rank > 11){
					leaderRank += "- \n";
					leaderScores += "- \n";
					leaderNames += "- \n";
				}
				leaderNames += "</font><font color='#000000' style='bold'>";
				leaderScores += "</font><font color='#000000' style='bold'>";
				leaderRank += xmlGameEnd.firstChild.childNodes[count].attributes.rank +"\n";
				leaderNames += xmlGameEnd.firstChild.childNodes[count].attributes.name + "\n";
				leaderScores += xmlGameEnd.firstChild.childNodes[count].attributes.score + "\n";
				leaderNames += "</font><font color='#FFFFFF' style='bold'>"
				leaderScores += "</font><font color='#FFFFFF' style='bold'>"
			}
			else {
				leaderRank += xmlGameEnd.firstChild.childNodes[count].attributes.rank +"\n";
				leaderNames += xmlGameEnd.firstChild.childNodes[count].attributes.name + "\n";
				leaderScores += xmlGameEnd.firstChild.childNodes[count].attributes.score + "\n";
			}
		}
		
		leaderNames += "</font>"
		leaderScores += "</font>"
		
		_sdtLeaderRank.text = leaderRank
		_sdtLeaderNames.htmlText = leaderNames
		_sdtLeaderScores.htmlText = leaderScores
		//trace(leaderNames);
	}
	
	public function openMoreLeaderboards(){
		var sMoreLeaderBoardURL = Main.getInstance()._cAppConfigFile.getData("moreLeaderboardsUrl");
		if(sMoreLeaderBoardURL==undefined)sMoreLeaderBoardURL = "http://72.3.138.97/ngmoreleaderboards/default.aspx"
		var jscommand:String = "window.open("+sMoreLeaderBoardURL+"','_blank','height=588,width=760');";
		//trace("javascript:"+jscommand);
		//getURL("javascript:" + jscommand + " void(0);");
		////getURL("javascript:window.open('"+sMoreLeaderBoardURL+"', '_blank', 'width=760px,height=588px,status=0,resizable=1,location=0');");
		getURL(sMoreLeaderBoardURL+"?userName=" + DataManager.getInstance()._sUserName,"_blank");
		////var jscommand:String = "window.open('http://www.adobe.com','win','height=200,width=300,toolbar=no,scrollbars=yes');"; getURL("javascript:" + jscommand + " void(0);");
	}

	
}